
## this is write by qingluan 
# just a inti handler 
# and a tempalte offer to coder

import tornado
import tornado.web
import os

class BaseHandler(tornado.web.RequestHandler):
	def prepare(self):
		self.db = self.settings['db']
		self.static_path = self.settings['static_path']
		self.update_path = self.static_path


	def get_current_user(self):
		return (self.get_cookie('user'),self.get_cookie('passwd'))
	def get_current_secure_user(self):
		return (self.get_cookie('user'),self.get_secure_cookie('passwd'))
	def set_current_seccure_user_cookie(self,user,passwd):
		self.set_cookie('user',user)
		self.set_secure_cookie("passwd",passwd)


	def add_message(self,**args):
		
		def _get_args(di):
			return (di['to'],di['content'],di['writer'],di['path'])
		sql = "insert into message values (null,'%s','%s','%s','%s',date());" % _get_args(args)
		print sql
		con = self.db.connect("Prey.db")
		cur = con.cursor()
		cur.execute(sql)
		con.commit()
		con.close()

	def find_all(self):
		sql = "select * from message"
		con = self.db.connect("Prey.db")
		cur = con.cursor()
		cur.execute(sql)
		res = cur.fetchall()
		con.close()
		return res

	def file_update(self,argument,**kargs):
		self.file_meta = self.request.files[argument]

		def _path(name):
			return os.path.join(self.update_path,name)

		file_name = None
		for meta in self.file_meta:
			file_name = meta['filename']
			path = _path(file_name)
			with open(path,'wb') as up_fp:
				up_fp.write(meta['body'])

		return _path(file_name)




class IndexHandler(BaseHandler):
	
	def prepare(self):
		super(IndexHandler,self).prepare()
		self.template = "template/Index.html"

	def get(self):
		return self.render(self.template,post_page="/Index")

	@tornado.web.asynchronous
	def post(self):
		# you should get some argument from follow 
		post_args = self.get_argument("some_argument")
		# .....

		# self.redirect()  # redirect or reply some content
		self.write("hello world")
	


class MainHandler(BaseHandler):
	
	def prepare(self):
		super(MainHandler,self).prepare()
		self.template = "template/Main.html"

	def get(self):
		all_message  = self.find_all()
		if all_message:
			return self.render(self.template,post_page="/Main",**{
				"messages":all_message,
				"found":"ok"
				})
		else :
			return self.render(self.template,post_page="/Main",messages=all_message,found="NO")

	@tornado.web.asynchronous
	def post(self):
		# you should get some argument from follow 
		post_args = self.get_argument("some_argument")
		# .....

		# self.redirect()  # redirect or reply some content
		self.write("hello world")
	


class EmailHandler(BaseHandler):
	
	def prepare(self):
		super(EmailHandler,self).prepare()
		self.template = "template/Email.html"

	def get(self):
		return self.render(self.template,post_page="/Email")

	@tornado.web.asynchronous
	def post(self):
		# you should get some argument from follow 
		_to = self.get_argument("to")
		_content = self.get_argument("content")
		_writer = self.get_argument("writer")
		_bac_path  = self.file_update("file")
		_bac_path = _bac_path[1:]

		self.add_message(**{
				'to':_to,
				'writer':_writer,
				'content':_content,
				'path':_bac_path,
			})
		self.redirect("/main")
		# .....

		# self.redirect()  # redirect or reply some content
		#self.write("hello world")
	